package com.example.sports_meet.volunteer;


import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.sports_meet.MyDbHelper;
import com.example.sports_meet.R;

public class Volunteer_changeMessage extends AppCompatActivity {

    private EditText editGName, editGContent;
    private Button btnSub;
    private Spinner spinner;
    SQLiteDatabase db;
    MyDbHelper myDbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_volunteer_change_message);
        //managerAthleteHelper=new ManagerAthleteHelper(Volunteer_changeMessage.this);
        myDbHelper = new MyDbHelper(Volunteer_changeMessage.this, "Sports.db", null, 1);
        editGName = (EditText) findViewById(R.id.editgname);
        editGContent = (EditText) findViewById(R.id.editgcontent);
        btnSub = (Button) findViewById(R.id.btnsub);
        final String[] GGX = new String[]{"姓名", "性别", "年龄", "编号"};
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(Volunteer_changeMessage.this, android.R.layout.simple_spinner_item, GGX);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setAdapter(arrayAdapter);

        btnSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ContentValues values;
                String name = editGName.getText().toString();
                String news = editGContent.getText().toString();
                String str = spinner.getSelectedItem().toString();
                if (str.equals("编号")) {
                    //db=managerAthleteHelper.getWritableDatabase();
                    db = myDbHelper.getWritableDatabase();
                    values = new ContentValues();
                    values.put("vno", news);
                    db.update("volunteer", values, "vname=?", new String[]{name});
                    Toast.makeText(Volunteer_changeMessage.this, "信息更新成功", Toast.LENGTH_LONG).show();
                    db.close();
                } else if (str.equals("性别")) {
                    //db=managerAthleteHelper.getWritableDatabase();
                    db = myDbHelper.getWritableDatabase();
                    values = new ContentValues();
                    values.put("vsex", news);
                    db.update("volunteer", values, "vname=?", new String[]{name});
                    Toast.makeText(Volunteer_changeMessage.this, "信息更新成功", Toast.LENGTH_LONG).show();
                    db.close();
                } else if (str.equals("姓名")) {
                    // db=managerAthleteHelper.getWritableDatabase();
                    db = myDbHelper.getWritableDatabase();
                    values = new ContentValues();
                    values.put("vname", news);
                    db.update("volunteer", values, "vname=?", new String[]{name});
                    Toast.makeText(Volunteer_changeMessage.this, "信息更新成功", Toast.LENGTH_LONG).show();
                    db.close();
                } else if (str.equals("年龄")) {
                    Integer a = Integer.valueOf(news);
                    // db=managerAthleteHelper.getWritableDatabase();
                    db = myDbHelper.getWritableDatabase();
                    values = new ContentValues();
                    values.put("vage", a);
                    db.update("volunteer", values, "vname=?", new String[]{name});
                    Toast.makeText(Volunteer_changeMessage.this, "信息更新成功", Toast.LENGTH_LONG).show();
                    db.close();
                }

            }
        });
    }
}